var app = getApp();
Page({
  data: {
    chat: null,
    reversalChat: null
  },
  onLoad: function(options) {
    wx.showToast({
      title: '建议同城免费领养，请勿异地领养，避免遇到诈骗',
      icon: 'none',
      duration: 2000
    })
    //找到系统状态位置
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15,
      windowHeight: app.globalData.systemInfo.windowHeight,
      user: app.globalData.user
    })

    var chat = app.globalData.chat;
    for (var i of chat) {
      if (i.id == options.id) {
        this.setData({
          chat: i
        })
        var str = JSON.stringify(this.data.chat.chatList);
        str = JSON.parse(str);
        this.setData({
          reversalChat: str.reverse()
        })
      }
    }
    this.socketMessage();
  },
  onPullDownRefresh: function() {
    var that = this;
    var chat = this.data.chat;
    var chatList = chat.chatList;
    var lastChat = chatList[chatList.length - 1];
    var id = chat.id;
    var messageDate = lastChat.messageDate;
    wx.request({
      url: app.globalData.requestUrl + 'getChatList',
      data: {
        id: id,
        messageDate: messageDate
      },
      success: function(r) {
        wx.stopPullDownRefresh();
        var data = r.data.data;
        if (data.length == 0) {
          return;
        }
        chat.chatList = chatList.concat(data);
        that.setData({
          chat: chat
        })
        var str = JSON.stringify(that.data.chat.chatList);
        str = JSON.parse(str);
        that.setData({
          reversalChat: str.reverse()
        })
      },
      fail: function() {
        wx.showToast({
          title: '网络连接断开,加载消息失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  onReady: function() {
    this.go(2000);
  },
  //监听客户端发送消息
  socketMessage: function() {
    var that = this;
    wx.onSocketMessage(function(r) {
      var message = JSON.parse(r.data);

      var key = Object.keys(message)[0];
      if (key == 'putChat') {
        var chat = message[key];
        that.data.chat.chatList.unshift(chat);
        that.setData({
          chat: that.data.chat,
          content: ''
        })
        var str = JSON.stringify(that.data.chat.chatList);
        str = JSON.parse(str);
        that.setData({
          reversalChat: str.reverse()
        })
      }
    })
  },

  //返回下一个页面
  rollback: function() {
    //获取页面值栈
    var pages = getCurrentPages();
    wx.navigateBack({
      delta: 1
    })
  },
  //键入文字
  focusInput: function(e) {
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    if (cursor > 50) {
      value = value.substr(0, 50);
    }
    this.setData({
      content: value
    })
  },
  //发送
  comment: function() {
    if (this.data.content == '') {
      return;
    }
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'addChat',
      method: 'POST',
      data: {
        senderId: app.globalData.user.id,
        consigneeId: that.data.chat.userId,
        message: that.data.content
      },
      success: function(r) {
        if (r.data.code == 200) {
          var chat = {
            id: app.globalData.user.id + that.data.chat.userId,
            senderId: that.data.user.plainText,
            consigneeId: that.data.chat.userId,
            message: that.data.content,
          }
          that.data.chat.chatList.unshift(chat);
          that.setData({
            chat: that.data.chat,
            content: ''
          })
          var str = JSON.stringify(that.data.chat.chatList);
          str = JSON.parse(str);
          that.setData({
            reversalChat: str.reverse()
          })

        } else {
          wx.showToast({
            title: '发送私信失败',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络连接断开,发送消息失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  go: function(scrollTop) {
    wx.pageScrollTo({
      scrollTop: scrollTop,
      duration: 0
    })
  },
})